//====================================================
//
// Copyright (C) 2013 Samarjeet Singh
//
//====================================================

namespace SS.Ynote.Classic
{
       #region Using Directives

       using System;
       using System.Text;
       using System.IO;
       using System.Drawing;
       using System.Threading;
       using System.Globalization;
       using System.Windows.Forms;
       using System.Linq;
       using System.Xml;
       using System.ComponentModel.Composition;
       using System.Collections.Generic;
       using System.Text.RegularExpressions;
       using FastColoredTextBoxNS;
       using WeifenLuo.WinFormsUI.Docking;
       using SS.Ynote.Classic.Properties;
       using System.ComponentModel.Composition.Hosting;

        #endregion

    public partial class Main : Form, IYnoteHost
    {
        #region Private Variables
        /// <summary>
        /// number of panes
        /// </summary>
        int num = 1;
        /// <summary>
        /// Information Timer
        /// </summary>
        private System.Windows.Forms.Timer infotimer;
        /// <summary>
        /// Clipboard History
        /// </summary>
        private ClipboardHistory CH = new ClipboardHistory();
        /// <summary>
        /// Syntax Highlighter
        /// </summary>
        SyntaxHighlighter Highlighter;
        /// <summary>
        /// MRU List
        /// </summary>
        /// <summary>
        /// how many list will save
        /// </summary>
        const int MRUnumber = 6;
        /// <summary>
        /// MRU List
        /// </summary>
        Queue<string> MRUlist = new Queue<string>();

        #endregion

        #region Constructor

        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="path"></param>
        public Main(string path)
        {
            InitializeComponent();
            InitTimerandEvents();
            BuildLanguageMenu();
            LoadThemes();
            PerformFunction(path);
        }
        /// <summary>
        /// Renders according to the theme
        /// </summary>
        /// <param name="renderer"></param>
        void Render(ToolStripRenderer renderer)
        {
            ToolStripManager.Renderer = renderer;
            statusStrip.Renderer = renderer;
        }
        void LoadTheme(string name) 
        {
            foreach (ToolStripMenuItem item in mienvironmenttheme.DropDownItems)
                if (item.Text == name)
                    item.PerformClick();
        }
        /// <summary>
        /// Performs the function
        /// </summary>
        /// <param name="path"></param>
        void PerformFunction(string path) 
        {
            SettingsReader.LoadAllSetting();
            try
            {
                if (!string.IsNullOrEmpty(Settings.Default.Theme))
                    GetControlByName(Settings.Default.Theme, mienvironmenttheme).PerformClick();
                else
                    YnoteSettingsBase.CurrentTheme = new Themes.IDefaultTheme();
                this.dockPanel.Theme = new VS2012LightTheme();
                Highlighter = new SyntaxHighlighter();
                this.editToolStripMenuItem.DropDownOpening += new EventHandler(editToolStripMenuItem_DropDownOpening);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
                Render(Settings.Default.UITheme);
                miencodingsave.DropDownItems.AddRange(BuildEncodingList(miencodingsave_DropDownItemClicked));
                miEncodingreopen.DropDownItems.AddRange(BuildEncodingList(miEncodingreopen_DropDownItemClicked));
                if (path == string.Empty)
                {
                    CreateTab("New" + num);
                    num++;
                }
                else
                    OpenFile(path, 0);
                dockPanel.DocumentTabStripLocation = YnoteSettingsBase.TabLocation;
            }
            catch (Exception)
            {

            }
        }
        /// <summary>
        /// Initializes Timer and Events
        /// </summary>
        private void InitTimerandEvents()
        {
            this.openToolStripMenuItem.Image = this.openToolStripButton.Image;
            this.newToolStripMenuItem.Image = this.newToolStripButton.Image;
            this.cutToolStripMenuItem.Image = this.cutToolStripButton.Image;
            this.copyToolStripMenuItem.Image = this.copyToolStripButton.Image;
            this.pasteToolStripMenuItem.Image = this.pasteToolStripButton.Image;
            this.printToolStripMenuItem.Image = this.printToolStripButton.Image;
            this.saveToolStripMenuItem.Image = this.saveToolStripButton.Image;
            infotimer = new System.Windows.Forms.Timer();
            infotimer.Interval = 10;
            infotimer.Enabled = true;
            infotimer.Tick += new EventHandler(infotimer_Tick);
            infotimer.Start();
        }
        private void miencodingsave_DropDownItemClicked(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            Save(FromString(item.Name));
        }
        private void miEncodingreopen_DropDownItemClicked(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            try
            {
                if (ActiveEditor.Name == "Editor")
                {
                    OpenFileDialog o = new OpenFileDialog();
                    o.Filter = "";
                    o.ShowDialog();
                    if (!string.IsNullOrEmpty(o.FileName))
                        OpenFile(o.FileName, FromString(item.Name));
                }
                else
                    OpenFile(ActiveEditor.Name, FromString(item.Name));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void infotimer_Tick(object sender, EventArgs e)
        {
            try
            {
                var nCol = ActiveEditor.codebox.Selection.Start.iChar + 1;
                int Line = ActiveEditor.codebox.Selection.Start.iLine + 1;
                this.info.Text = " Line : " + Line + "   Col : " + nCol + "   Sel : " + ActiveEditor.codebox.Selection.Text.Length + "   Size : " + ActiveEditor.codebox.Text.Length + " byte(s)";
            }
            catch
            {
                // Do Nothing
            }
        }
        #endregion

        #region Properties

        /// <summary>
        /// Gets the Active Editor
        /// </summary>
        public Editor ActiveEditor { get { return dockPanel.ActiveDocument as Editor; } }
        /// <summary>
        /// Dock Panel
        /// </summary>
        public DockPanel Panel { get { return dockPanel; } }

        #endregion

        #region Helper Methods
        /// <summary>
        /// File Open Method
        /// </summary>
        /// <param name="file"></param>
        /// <param name="codepage"></param>
        public void OpenFile(string file, int codepage)
        {
            try
            {
                string ext = System.IO.Path.GetExtension(file);
                Editor edit = new Editor(this);
                edit.Text = Path.GetFileName(file);
                edit.Name = file;
                if (codepage == 0)
                    edit.codebox.Text = File.ReadAllText(file);
                else
                    edit.codebox.Text = File.ReadAllText(file, Encoding.GetEncoding(codepage));
                edit.codebox.ClearUndo();
                edit.codebox.IsChanged = false;
                
                edit.Show(dockPanel, DockState.Document);
                SaveRecentFile(file, mirecentfiles);
                GetControlByName(LanguageFromExtension(Path.GetExtension(file)).ToString(), misyntaxhighlight).PerformClick();
            }
            catch (System.Exception ex) { 
                MessageBox.Show(ex.Message); 
            }
        }
        public SyntaxHighlighter SyntaxHighlighter { get { return Highlighter; } }

        Language LanguageFromExtension(string extension)
        {
            if (YnoteSettingsBase.HTMLExtensions.Contains(extension))
                return Language.HTML;
            else if (YnoteSettingsBase.ActionscriptExtensions.Contains(extension))
                return Language.Actionscript;
            else if (YnoteSettingsBase.AssemblyExtensions.Contains(extension))
                return Language.Assemblyx86;
            else if (YnoteSettingsBase.BatchExtensions.Contains(extension))
                return Language.Batch;
            else if (YnoteSettingsBase.CPPExtensions.Contains(extension))
                return Language.CPP;
            else if (YnoteSettingsBase.CSExtensions.Contains(extension))
                return Language.CSharp;
            else if (YnoteSettingsBase.CSSExtensions.Contains(extension))
                return Language.CSS;
            else if (YnoteSettingsBase.DExtensions.Contains(extension))
                return Language.D;
            else if (YnoteSettingsBase.FSharpExtensions.Contains(extension))
                return Language.FSharp;
            else if (YnoteSettingsBase.INIExtensions.Contains(extension))
                return Language.INI;
            else if (YnoteSettingsBase.JavaExtensions.Contains(extension))
                return Language.Java;
            else if (YnoteSettingsBase.JScriptExtensions.Contains(extension))
                return Language.Javascript;
            else if (YnoteSettingsBase.JSONExtension.Contains(extension))
                return Language.JSON;
            else if (YnoteSettingsBase.LispExtensions.Contains(extension))
                return Language.Lisp;
            else if (YnoteSettingsBase.LuaExtensions.Contains(extension))
                return Language.Lua;
            else if (YnoteSettingsBase.OpenCLExtensions.Contains(extension))
                return Language.OpenCL;
            else if (YnoteSettingsBase.PascalExtensions.Contains(extension))
                return Language.Pascal;
            else if (YnoteSettingsBase.PerlExtensions.Contains(extension))
                return Language.Perl;
            else if (YnoteSettingsBase.PHPExtensions.Contains(extension))
                return Language.PHP;
            else if (YnoteSettingsBase.PythonExtensions.Contains(extension))
                return Language.Python;
            else if (YnoteSettingsBase.RubyExtension.Contains(extension))
                return Language.Ruby;
            else if (YnoteSettingsBase.ScalaExtensions.Contains(extension))
                return Language.Ruby;
            else if (YnoteSettingsBase.SQLExtensions.Contains(extension))
                return Language.SQL;
            else if (YnoteSettingsBase.VBExtensions.Contains(extension))
                return Language.VB;
            else if (YnoteSettingsBase.XMLExtensions.Contains(extension))
                return Language.Xml;
            else if (YnoteSettingsBase.QBasicExtensions.Contains(extension))
                return Language.QBasic;
            else if (YnoteSettingsBase.ASPExtensions.Contains(extension))
                return Language.ASP;
            else if (YnoteSettingsBase.CExtensions.Contains(extension))
                return Language.C;
            return Language.Text;
        }
        void LoadUserSyntax(string dir)
        {
            foreach (string s in Directory.GetFiles(dir))
                ConstructUserLanguage(Path.GetFileNameWithoutExtension(s), s);
        }
        void ConstructUserLanguage(string lang, string descriptionfile)
        {
            ToolStripMenuItem item = new ToolStripMenuItem(lang);
            item.Name = descriptionfile;
            item.Click += new EventHandler(usersyntaxitem_Click);
            misyntaxhighlight.DropDownItems.Add(item);
        }
        private void usersyntaxitem_Click(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem item in misyntaxhighlight.DropDownItems)
                item.Checked = false;
            ToolStripMenuItem clickeditem = sender as ToolStripMenuItem;
            clickeditem.Checked = true;
            ActiveEditor.codebox.Language = Language.Text;
            ActiveEditor.codebox.ClearStyle(StyleIndex.All);
            ActiveEditor.codebox.DescriptionFile = clickeditem.Name;
        }
        private Language FromExtension(string extension)
        {
            if (YnoteSettingsBase.HTMLExtensions.Contains(extension))
                return Language.HTML;
             else if (YnoteSettingsBase.BatchExtensions.Contains(extension))
                     return Language.Batch;
             else if (YnoteSettingsBase.CSSExtensions.Contains(extension))
                     return Language.CSS;
             else if (YnoteSettingsBase.CPPExtensions.Contains(extension))
                     return Language.CPP;
             else if (YnoteSettingsBase.PHPExtensions.Contains(extension))
                      return Language.PHP;
             else if (YnoteSettingsBase.PythonExtensions.Contains(extension))
                     return Language.Python;
             else if (YnoteSettingsBase.RubyExtension.Contains(extension))
                     return Language.Ruby;
             else if (YnoteSettingsBase.SQLExtensions.Contains(extension))
                     return Language.SQL;
             else if (YnoteSettingsBase.VBExtensions.Contains(extension))
                     return Language.VB;
             else if (YnoteSettingsBase.XMLExtensions.Contains(extension))
                     return Language.Xml;
             else if (YnoteSettingsBase.CSExtensions.Contains(extension))
                     return Language.CSharp;
             else if (YnoteSettingsBase.INIExtensions.Contains(extension))
                     return Language.INI;
             else if (YnoteSettingsBase.LuaExtensions.Contains(extension))
                     return Language.Lua;
             else if (YnoteSettingsBase.JScriptExtensions.Contains(extension))
                      return Language.Javascript;
             else if (YnoteSettingsBase.JSONExtension.Contains(extension))
                return Language.JSON;
            return Language.Text;
        }
        void BuildLanguageMenu()
        {
            foreach (FastColoredTextBoxNS.Language lang in Enum.GetValues(typeof(FastColoredTextBoxNS.Language)).Cast<FastColoredTextBoxNS.Language>())
            {
                ToolStripMenuItem m = new ToolStripMenuItem();
                m.Text = lang.ToString();
                m.Click += new EventHandler(m_Click);
                misyntaxhighlight.DropDownItems.Add(m);
            }
            GetControlByName("Text", misyntaxhighlight).Checked = true;

        }
        private void OnLoad()
        {
            LoadRecentList();
            foreach (string item in MRUlist)
            {
                System.Windows.Forms.ToolStripMenuItem fileRecent = new System.Windows.Forms.ToolStripMenuItem(item);  //create new menu for each item in list
                this.mirecentfiles.DropDownItems.Add(fileRecent); //add the menu to "recent" menu
            }
        }
        private ToolStripMenuItem[] BuildEncodingList(EventHandler handler)
        {
            List<ToolStripMenuItem> items = new List<ToolStripMenuItem>();
            foreach (EncodingInfo info in Encoding.GetEncodings())
            {
                ToolStripMenuItem i1 = new ToolStripMenuItem(info.DisplayName, null, handler);
                i1.Name = info.CodePage.ToString();
                items.Add(i1);
            }
            return items.ToArray();
        }
        private void CreateTab(string title)
        {
            Editor edit = new Editor(this);
            edit.Text = title;
            edit.Show(dockPanel, DockState.Document);
        }
        private void CreateTab(string title, string text)
        {
            Editor edit = new Editor(this);
            edit.Text = title;
            edit.codebox.Text = text;
            edit.Show(dockPanel, DockState.Document);
        }
        /// <summary>
        /// Int From String
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        static int FromString(string s)
        {
            int i;
            int.TryParse(s, out i);
            return i;
        }
        /// <summary>
        /// Sets UI Mode
        /// </summary>
        /// <param name="mode"></param>
       
        #region File Binding

        void OpenBinding(string name)
        {
            Editor edit = new Editor(this);
            edit.Text = Path.GetFileName(name);
            edit.Name = name;
            edit.codebox.OpenBindingFile(name, Encoding.Default);
            edit.codebox.IsChanged = false;
            edit.Show(dockPanel, DockState.Document);
        }
        void SaveBinding(Editor edit)
        {
            if (edit.Name != "Editor")
                edit.codebox.SaveToFile(edit.Name, Encoding.Default);
            else
            {
                SaveFileDialog s = new SaveFileDialog();
                s.Filter = filefilter;
                s.ShowDialog();
                if (!string.IsNullOrEmpty(s.FileName))
                    edit.codebox.SaveToFile(s.FileName, Encoding.Default);
            }
        }
        void SaveBinding(Editor edit, string title) 
        {
            if (edit.Name != "Editor")
                edit.codebox.SaveToFile(edit.Name, Encoding.Default);
            else
            {
                SaveFileDialog s = new SaveFileDialog();
                s.Filter = filefilter;
                s.Title = title;
                s.ShowDialog();
                if (!string.IsNullOrEmpty(s.FileName))
                    edit.codebox.SaveToFile(s.FileName, Encoding.Default);
            }
        }
        void SaveBinding(Editor edit, int codepage)
        {
            if (edit.Name != "Editor")
                edit.codebox.SaveToFile(edit.Name, Encoding.GetEncoding(codepage));
            else
            {
                SaveFileDialog s = new SaveFileDialog();
                s.Filter = filefilter;
                s.ShowDialog();
                if (!string.IsNullOrEmpty(s.FileName))
                    edit.codebox.SaveToFile(s.FileName, Encoding.GetEncoding(codepage));
            }
        }
        #endregion
        /// <summary>
        /// store a list to file and refresh list
        /// </summary>
        /// <param name="path"></param>
        private void SaveRecentFile(string path, System.Windows.Forms.ToolStripMenuItem recent)
        {
            recent.DropDownItems.Clear(); //clear all recent list from menu
            LoadRecentList(); //load list from file
            if (!(MRUlist.Contains(path))) //prevent duplication on recent list
                MRUlist.Enqueue(path); //insert given path into list
            while (MRUlist.Count > MRUnumber) //keep list number not exceeded given value
            {
                MRUlist.Dequeue();
            }
            foreach (string item in MRUlist)
            {
                System.Windows.Forms.ToolStripMenuItem fileRecent = new System.Windows.Forms.ToolStripMenuItem(item, null);  //create new menu for each item in list
                recent.DropDownItems.Add(fileRecent); //add the menu to "recent" menu
            }
            //writing menu list to file
            StreamWriter stringToWrite = new StreamWriter(System.Windows.Forms.Application.StartupPath + "\\Recent.info"); //create file called "Recent.txt" located on app folder
            foreach (string item in MRUlist)
            {
                stringToWrite.WriteLine(item); //write list to stream
            }
            stringToWrite.Flush(); //write stream to file
            stringToWrite.Close(); //close the stream and reclaim memory
        }
        /// <summary>
        /// load recent file list from file
        /// </summary>
        void LoadRecentList()
        {
            MRUlist.Clear();
            try
            {
                StreamReader listToRead = new StreamReader(System.Windows.Forms.Application.StartupPath + "\\Recent.info"); //read file stream
                string line;
                while ((line = listToRead.ReadLine()) != null) //read each line until end of file
                    MRUlist.Enqueue(line); //insert to list
                listToRead.Close(); //close the stream
            }
            catch (Exception)
            {
                //throw;
            }

        }
        /// <summary>
        /// Duplicate Line
        /// </summary>
        /// <param name="iLine"></param>
        /// <param name="fctb"></param>
        void DuplicateLine(int iLine, FastColoredTextBox fctb)
        {
            fctb.Selection.Start = new Place(0, iLine);
            fctb.Selection.Expand();
            object text = fctb.Selection.Text;
            fctb.Selection.Start = new Place(0, iLine);
            fctb.InsertText(text + "\r\n" + "\r\n");
        }

        private void LineOperate(Operation o, int iLine, FastColoredTextBox fctb)
        {
            fctb.Selection.Start = new Place(0, iLine);
            fctb.Selection.Expand();
            if (o == Operation.Cut)
                fctb.Cut();
            else if (o == Operation.Copy)
                fctb.Copy();
            else if (o == Operation.Split)
            {
                new Utils.InsertLine(ActiveEditor.codebox, Utils.InsertType.Splitter).Show();
            }
        }
        /// <summary>
        /// On Syntax Highlight Select
        /// </summary>
        /// <param name="item"></param>
        void OnSelect(ToolStripMenuItem item)
        {
            foreach (ToolStripMenuItem t in misyntaxhighlight.DropDownItems)
            {
                t.Checked = false;
            }
            item.Checked = true;

            Highlighter.SetSyntax((Language)Enum.Parse(typeof(FastColoredTextBoxNS.Language), item.Text), ActiveEditor.codebox.Range);
        }
        /// <summary>
        /// Get ToolStripMenuItem by name
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        static ToolStripMenuItem GetControlByName(string Name, ToolStripMenuItem parent)
        {
            foreach (ToolStripMenuItem c in parent.DropDownItems)
                if (c.Text == Name)
                    return c;
            return null;
        }
        /// <summary>
        /// Get Editor by name
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        Editor GetEditorByName(string Name)
        {
            foreach (Editor edit in dockPanel.Documents)
                if (edit.Name == Name)
                    return edit;
            return null;
        }
        /// <summary>
        /// Save As
        /// </summary>
        private void SaveAs()
        {
            try
            {
                using (SaveFileDialog s = new SaveFileDialog())
                {
                    s.Filter = filefilter;
                    s.ShowDialog();
                    if (!string.IsNullOrEmpty(s.FileName))
                    {
                        ActiveEditor.codebox.SaveToFile(s.FileName, Encoding.Default);
                        ActiveEditor.Text = Path.GetFileName(s.FileName);
                        ActiveEditor.Name = s.FileName;
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// Save As With Encoding
        /// </summary>
        /// <param name="codepage"></param>
        public void SaveAs(int codepage)
        {
            try
            {
                using (SaveFileDialog s = new SaveFileDialog())
                {
                    s.Filter = filefilter;
                    s.ShowDialog();
                    if (!string.IsNullOrEmpty(s.FileName))
                    {
                        ActiveEditor.codebox.SaveToFile(s.FileName, Encoding.GetEncoding(codepage));
                        ActiveEditor.Text = Path.GetFileName(s.FileName);
                        ActiveEditor.Name = s.FileName;
                    }
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// Save
        /// </summary>
        public void Save()
        {
            Stats.Text = "Saving File......";
            if (!(ActiveEditor.Name == "Editor"))
            {
                SaveBinding(ActiveEditor);
                ActiveEditor.Text = ActiveEditor.Text.Replace("*", string.Empty);
                Stats.Text = "Ready";
            }
            else
            {
                SaveAs();
                Stats.Text = "Ready";
            }
        }
        /// <summary>
        /// Save With CodePage
        /// </summary>
        /// <param name="codepage"></param>
        public void Save(int codepage)
        {

            Stats.Text = "Saving File......";
            if (!(ActiveEditor.Name == "Editor"))
            {
                SaveBinding(ActiveEditor, codepage);
                ActiveEditor.Text = ActiveEditor.Text.Replace("*", string.Empty);
                Stats.Text = "Ready";
            }
            else
            {
                SaveAs(codepage);
                Stats.Text = "Ready";
            }
        }
        /// <summary>
        /// Zoom With Error Handling
        /// </summary>
        /// <param name="percent"></param>
        void Adjustzoom(int percent)
        {
            try
            {
                ActiveEditor.codebox.Zoom = percent;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        void OnLineEndingChanged(string ending)
        {
            ActiveEditor.codebox.NewLine = ending;
        }
       
        private void Render(UITheme theme)
        {
            switch(theme)
            {
                case UITheme.System: Render(new ToolStripAeroRenderer(ToolbarTheme.Toolbar));
                    dockPanel.Theme = new VS2012LightTheme();
                    break;
                case UITheme.Light: Render(new Themes.MetroTheme());
                     dockPanel.Theme = new VS2012LightTheme();
                    break;
                case UITheme.Dark: Render(new Themes.DarkUITheme());
                    dockPanel.Theme = new VS2012DarkTheme();
                    break;
                case UITheme.ProfessionalSystem: ToolStripManager.RenderMode = ToolStripManagerRenderMode.System;
                    dockPanel.Theme = new VS2005Theme();
                    break;
            }
        }

        /// <summary>
        /// Open Session
        /// </summary>
        /// <param name="file"></param>
        private void Opensession(string file)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(file);
            foreach (XmlNode item in xmldoc.SelectNodes("*/ynotesession/file"))
            {
                OpenFile(item.InnerText, 0);
            }
        }
        /// <summary>
        /// Save Session
        /// </summary>
        /// <param name="path"></param>
        private void SaveSession(string path)
        {
            string Document = "<?xml version='1.0'?>\n\r<doc>\n\r<ynotesession>\n\r";
            foreach (Editor doc in dockPanel.Documents)
            {
                if (doc.Name != "Editor")
                {
                    Document += "<file>" + doc.Name + "</file>\n\r";
                }
            }
            Document += "</ynotesession>\n\r</doc>";
            File.WriteAllText(path, Document);
        }
        #endregion

        #region Plugins and Extensibility

        [ImportMany("IYnotePlugin")]
        private IEnumerable<IYnotePlugin> GeneralPlugins { get; set; }

        [ImportMany("IYnoteThemeExtension")]
        private IEnumerable<IYnoteThemeExtension> Themes { get; set; }
        
        void LoadPlugins()
        {
            var dircatalog = new DirectoryCatalog(Application.StartupPath + "\\Plugins");
            var container = new CompositionContainer(dircatalog);
            GeneralPlugins = container.GetExportedValues<IYnotePlugin>();
            foreach (var plugin in GeneralPlugins)
            {
                plugin.Host = this;
                plugin.Dispose();
                switch (plugin.AddTo)
                {
                    case AddTo.File: ConstructIGeneralPlugin(fileToolStripMenuItem, plugin);
                        break;
                    case AddTo.Edit: ConstructIGeneralPlugin(editToolStripMenuItem, plugin);
                        break;
                    case AddTo.View: ConstructIGeneralPlugin(editToolStripMenuItem, plugin);
                        break;
                    case AddTo.Run: ConstructIGeneralPlugin(editToolStripMenuItem, plugin);
                        break;
                    case AddTo.Settings: ConstructIGeneralPlugin(settingsToolStripMenuItem, plugin);
                        break;
                    case AddTo.Plugins: ConstructIGeneralPlugin(pluginsToolStripMenuItem, plugin); pluginsToolStripMenuItem.Visible = true;
                        break;
                }
                IPluginManager.Plugins.Add(plugin);
            }
        }
        void LoadThemes() 
        {
            var dircatalog = new DirectoryCatalog(Application.StartupPath + "\\Themes");
            var container = new CompositionContainer(dircatalog);
            Themes = container.GetExportedValues<IYnoteThemeExtension>();
            foreach (IYnoteThemeExtension Theme in Themes)
                ConstructIThemeItem(Theme);
        }
        void ConstructIThemeItem(IYnoteThemeExtension Extension)
        {
            ThemeMenuItem item = new ThemeMenuItem();
            item.Name = Extension.Name;
            item.Text = Extension.Name;
            item.EmbeddedTheme = Extension.Theme;
            item.Click += new EventHandler(themeitem_Click);
            mienvironmenttheme.DropDownItems.Add(item);
        }
        private void themeitem_Click(object sender, EventArgs e) 
        {
            ThemeMenuItem item = sender as ThemeMenuItem;
            foreach (ThemeMenuItem item1 in mienvironmenttheme.DropDownItems)
                item1.Checked = false;
            item.Checked = true;
            YnoteSettingsBase.CurrentTheme = item.EmbeddedTheme;
        }
        static void ConstructIGeneralPlugin(ToolStripMenuItem addto, IYnotePlugin plugin)
        {
            ToolStripMenuItem item = new ToolStripMenuItem();
            item.Text = plugin.Name;
            item.DropDownItems.AddRange(plugin.MenuItems.ToArray());
            addto.DropDownItems.Add(item);
        }
        #endregion

        #region Events

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateTab("New" + num); num++;
        }
        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ActiveEditor.codebox.Undo();
            }
            catch (Exception)
            {
            }
        }
        private void commaSeperatedValueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(ActiveEditor.codebox.Selection.Text, TextDataFormat.CommaSeparatedValue);
        }

        private void unicodeTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(ActiveEditor.codebox.Selection.Text, TextDataFormat.CommaSeparatedValue);
        }

        private void plainTextToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(ActiveEditor.codebox.Selection.Text, TextDataFormat.Text);
        }

        private void clearRecentFilesHistoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            File.Delete(Application.StartupPath + @"\Recent.info");
            mirecentfiles.DropDownItems.Clear();
        }
        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ActiveEditor.codebox.Redo();
            }
            catch (Exception)
            {
            }
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ActiveEditor.codebox.Cut();
            }
            catch(Exception)
            {
            }
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ActiveEditor.codebox.Copy();
            }
            catch (Exception)
            {
            }
        }


        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ActiveEditor.codebox.Paste();
            }
            catch (Exception)
            {
            }
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ActiveEditor.codebox.SelectAll();
            }
            catch (Exception)
            {
            }
        }

        private void clearAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ActiveEditor.codebox.Clear();
            }
            catch (System.Exception)
            {
            }
        }
        private void gotobookmark_DropDownOpening(object sender, EventArgs e)
        {
            gotobookmark.DropDownItems.Clear();
            foreach (var bookmark in ActiveEditor.codebox.Bookmarks)
            {
                var item = gotobookmark.DropDownItems.Add(bookmark.Name);
                item.Tag = bookmark;
                item.Click += (o, a) => ((Bookmark)(o as ToolStripItem).Tag).DoVisible();
            }
        }
        private string filefilter = "All Files(*.*)|*.*|Text Files(*.txt)|*.txt|XML Files (*.xml)|*.xml|XML Schema Definition File(*.xsd)|*.xsd|Log File (*.log)|*.log|HTML Document (*.html),(*.xhtml),(*.shtml)|*.html;*.xhtml;*.shtml|ASP.NET File(*.asp),(*.aspx)|*.asp;*.aspx|PHP Document (*.php)|*.php|Cascading Style Sheet (*.css)|*.css|Javascript File (*.js)|*.js|QBasic File(*.bas)|*.bas|Visual Basic File (*.vb)|*.vb|Python File (*.py)|*.py|Ruby File(*.ruby)|*.ruby|Lua File(*.lua)|Flash Actionscript file(*.as)|*.as|C# Source File(*.cs)|*.cs|C Source File(*.c)|C++ Source File (*.cpp)|*.cpp|C++ Header File(*.h)|*.h";
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog o = new OpenFileDialog())
            {
                o.Filter = filefilter;
                o.ShowDialog();
                foreach (string file in o.FileNames)
                    if (!string.IsNullOrEmpty(file))
                        OpenFile(file, 0);
            }
        }

        private void hTMLToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string HTML = ActiveEditor.codebox.Html;
            SaveFileDialog s = new SaveFileDialog();
            s.Title = "Export as HTML Document";
            s.Filter = "HTML Documents (*.html)|*.html";
            s.ShowDialog();
            if (s.FileName != null)
                File.WriteAllText(s.FileName, HTML);
        }

        private void rTFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string RTF = ActiveEditor.codebox.Rtf;
            SaveFileDialog s = new SaveFileDialog();
            s.Title = "Export as Rich Text Documents";
            s.Filter = "Rich Text Documents Documents (*.rtf)|*.rtf";
            s.ShowDialog();
            if (s.FileName != null)
            {
                File.WriteAllText(s.FileName, RTF);
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Save();
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stats.Text = "Loading Print Preview Dialog....";
            ActiveEditor.codebox.Print(new PrintDialogSettings());
            Stats.Text = "Ready";
        }

        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.ShowFindDialog();
        }

        private void replaceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.ShowReplaceDialog();
        }

        private void goToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.ShowGoToDialog();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new About().Show();
        }
        private void m_Click(object sender, EventArgs e)
        {
            OnSelect(sender as ToolStripMenuItem);
        }
        private void openCurrentDirInExplorerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Path.GetDirectoryName(ActiveEditor.Name));
        }
        private void cRLFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OnLineEndingChanged("\r\n");
        }

        private void cRToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OnLineEndingChanged("\r");
        }

        private void lFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OnLineEndingChanged("\n");
        }

        private void foldAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.CollapseAllFoldingBlocks();
        }

        private void unFoldAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.ExpandAllFoldingBlocks();
        }

        private void foldSelectedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.CollapseBlock(ActiveEditor.codebox.Selection.Start.iLine, ActiveEditor.codebox.Selection.End.iLine);
        }

        private void unFoldSelectedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.ExpandBlock(ActiveEditor.codebox.Selection.Start.iLine, ActiveEditor.codebox.Selection.End.iLine);
        }

        private void toUpperToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string upper = ActiveEditor.codebox.SelectedText.ToUpper();
                ActiveEditor.codebox.SelectedText = upper;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void toLowerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string lower = ActiveEditor.codebox.SelectedText.ToLower();
                ActiveEditor.codebox.SelectedText = lower;
            }
            catch (Exception)
            {
            }
        }
        private void hTMLToolStripMenuItem1_Click_1(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetText(ActiveEditor.codebox.Html);
            }
            catch (Exception) { }
        }

        private void rTFToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                Clipboard.SetText(ActiveEditor.codebox.Rtf);
            }
            catch (Exception) { }
        }

        private void transparentUIToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.transparentUIToolStripMenuItem.Checked == true)
                this.Opacity = 0.6;
            else if (this.transparentUIToolStripMenuItem.Checked == false)
                this.Opacity = 1.0;
        }

        private void cutLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LineOperate(Operation.Cut, ActiveEditor.codebox.Selection.Start.iLine, ActiveEditor.codebox);
        }

        private void copyLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LineOperate(Operation.Copy, ActiveEditor.codebox.Selection.Start.iLine, ActiveEditor.codebox);
        }

        private void splitLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LineOperate(Operation.Split, ActiveEditor.codebox.Selection.Start.iLine, ActiveEditor.codebox);
        }
        private void editToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
        {
            try
            {
                this.undoToolStripMenuItem.Enabled = ActiveEditor.UndoEnabled;
                this.redoToolStripMenuItem.Enabled = ActiveEditor.RedoEnabled;
                if (string.IsNullOrEmpty(Clipboard.GetText()))
                {
                    pasteToolStripMenuItem.Enabled = false;
                }
                else
                    pasteToolStripMenuItem.Enabled = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void rulerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (rulerToolStripMenuItem.Checked == true)
            {
                ActiveEditor.ruler1.Visible = true;
                ActiveEditor.ruler1.Enabled = true;
            }
            else if (rulerToolStripMenuItem.Checked == false)
            {
                ActiveEditor.ruler1.Visible = false;
                ActiveEditor.ruler1.Enabled = false;
            }
        }

        private void verticalLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Utils.InsertLine(ActiveEditor.codebox, SS.Ynote.Classic.Utils.InsertType.Width).Show();
        }

        private void selectLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fctb = ActiveEditor.codebox;
            fctb.Selection.Start = new Place(0, fctb.Selection.Start.iLine);
            fctb.Selection.Expand();
        }
        private void micase_DropDownOpening(object sender, EventArgs e)
        {
            try
            {
                if (ActiveEditor.codebox.SelectedText == null)
                {
                    toLowerToolStripMenuItem.Enabled = false;
                    toUpperToolStripMenuItem.Enabled = false;
                }
                else
                {
                    toLowerToolStripMenuItem.Enabled = true;
                    toUpperToolStripMenuItem.Enabled = true;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        private void moveSelectedLinesDownToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.MoveSelectedLinesDown();
        }

        private void moveSelectedLinesUpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.MoveSelectedLinesUp();
        }

        private void duplicateLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                DuplicateLine(ActiveEditor.codebox.Selection.Start.iLine, ActiveEditor.codebox);
            }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void startRecordingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ActiveEditor.codebox.MacrosManager.IsRecording = true;
                macrostart.Enabled = false;
                macrostop.Enabled = true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void stopRecordingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ActiveEditor.codebox.MacrosManager.IsRecording = false;
                macrostart.Enabled = true;
                macrostop.Enabled = false;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void executeMacrosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ActiveEditor.codebox.MacrosManager.ExecuteMacros();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void executeMacrosMultipleTimesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                new Utils.InsertLine(ActiveEditor.codebox, SS.Ynote.Classic.Utils.InsertType.Macro).Show();
            }
            catch { }
        }

        private void clearRecordedMacroDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ActiveEditor.codebox.MacrosManager.ClearMacros();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void hotKeysToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                var form = new HotkeysEditorForm(ActiveEditor.codebox.HotkeysMapping);
                if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    ActiveEditor.codebox.HotkeysMapping = form.GetHotkeys();
            }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }
        private void changedLineColourToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.ChangedLineColor = Color.FromArgb(60, Color.Blue);
        }
        private void mitoolbar_Click(object sender, EventArgs e)
        {
            toolstrip.Visible = mitoolbar.Checked;
        }

        private void mistatusbar_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = mistatusbar.Checked;
        }

        private void zoomInToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.Zoom = ActiveEditor.codebox.Zoom + 20; }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void zoomOutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.Zoom = ActiveEditor.codebox.Zoom - 20; }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void restoreDefaultZoomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.Zoom = 100; }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void setSelectedTextAsReadonlyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.Selection.ReadOnly = true; }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void setSelectedTextAsWritableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.Selection.ReadOnly = false; }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void charToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("charmap.exe");
        }

        private void hiddenCharsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (hiddenCharsToolStripMenuItem.Checked)
                    ActiveEditor.ShowHiddenCharacters = true;
                else
                    ActiveEditor.ShowHiddenCharacters = false;
                ActiveEditor.codebox.Invalidate();
            }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }

        }
        private void increaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.IncreaseIndent(); }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void decreaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.DecreaseIndent(); }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            foreach (ThemeMenuItem item in mienvironmenttheme.DropDownItems)
                if (item.Checked)
                    Settings.Default.Theme = item.Name;
            Settings.Default.Save();
            base.OnClosing(e);
        }
        private void dockPanel_ActiveContentChanged(object sender, EventArgs e)
        {
            try
            {
                CH.TextBox = ActiveEditor.codebox;
                if (dockPanel.Contents.Count == 0)
                {
                    this.editToolStripMenuItem.Enabled = false;
                    this.macrosToolStripMenuItem.Enabled = false;
                    this.zoomToolStripMenuItem.Enabled = false;
                    this.toolstrip.Enabled = false;
                }
                else
                {
                    this.editToolStripMenuItem.Enabled = true;
                    this.macrosToolStripMenuItem.Enabled = true;
                    this.zoomToolStripMenuItem.Enabled = true;
                    this.toolstrip.Enabled = true;
                }
            }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }
        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            newToolStripMenuItem_Click(sender, e);
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            openToolStripMenuItem_Click(sender, e);
        }

        private void printToolStripButton_Click(object sender, EventArgs e)
        {
            printToolStripMenuItem_Click(sender, e);
        }

        private void cutToolStripButton_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.Cut(); }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void copyToolStripButton_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.Copy(); }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void pasteToolStripButton_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.Paste(); }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void fullScreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.fullScreenToolStripMenuItem.Checked == true)
            {
                this.WindowState = FormWindowState.Maximized;
                this.FormBorderStyle = FormBorderStyle.None;
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.Sizable;
            }
        }

        private void runToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                string p = Path.GetTempFileName() + ".run";
                File.WriteAllText(p, ActiveEditor.codebox.Text);
                RunDialog r = new RunDialog(p);
                r.Show();
            }
            catch (System.Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void openCurrentDirCommandPromptToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("cmd.exe", ActiveEditor.Name);
            }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void googleSearchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.google.com/search?q=" + ActiveEditor.codebox.Text);

        }

        private void bingSearchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.bing.com/search?q=" + ActiveEditor.codebox.Text);

        }

        private void wikipediaSearchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://en.wikipedia.org/search?q=" + ActiveEditor.codebox.Text);
        }

        private void mirecentfiles_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.OpenFile(e.ClickedItem.Text, 0);
        }


        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Bookmarks.Add(ActiveEditor.codebox.Selection.Start.iLine);
        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Bookmarks.Remove(ActiveEditor.codebox.Selection.Start.iLine);
        }


        private void clipboardHistoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                //Specify the textbox
                CH.TextBox = ActiveEditor.codebox;
                CH.Show(dockPanel, DockState.DockRight);
            }
            catch (Exception) {
                new ClipboardHistory().Show(dockPanel, DockState.DockRight);
            }
        }
        private void launToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string tempfile = Path.GetTempFileName() + ".html";
                System.IO.File.WriteAllText(tempfile, ActiveEditor.codebox.Text);
                System.Diagnostics.Process.Start("iexplore.exe", tempfile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void launchInChromeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string tempfile = Path.GetTempFileName() + ".html";
                System.IO.File.WriteAllText(tempfile, ActiveEditor.codebox.Text);
                System.Diagnostics.Process.Start("chrome.exe", tempfile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void launchInFirefoxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string tempfile = Path.GetTempFileName() + ".html";
                System.IO.File.WriteAllText(tempfile, ActiveEditor.codebox.Text);
                System.Diagnostics.Process.Start("firefox.exe", tempfile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void launchInCommandPromptToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string AppDataDir = Path.GetTempFileName() + ".bat";
            System.IO.File.WriteAllText(AppDataDir, ActiveEditor.codebox.Text);
            System.Diagnostics.Process.Start("cmd.exe", AppDataDir);
        }

        private void visitProjectPageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://ynoteclassic.codeplex.com");
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            addToolStripMenuItem_Click(sender, e);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            removeToolStripMenuItem_Click(sender, e);
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SaveAs();
            ActiveEditor.codebox.IsChanged = false;
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void removeEmptyLinesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var iLines = ActiveEditor.codebox.FindLines(@"^\s*$", RegexOptions.None);
            ActiveEditor.codebox.RemoveLines(iLines);
        }

        private void goToEndToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.GoEnd(); }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void gotToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.GoHome(); ; }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            Adjustzoom(25);
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            Adjustzoom(50);
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            Adjustzoom(100);
        }

        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            Adjustzoom(150);
        }

        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            Adjustzoom(200);
        }

        private void toolStripMenuItem11_Click(object sender, EventArgs e)
        {
            Adjustzoom(300);
        }

        private void columnEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Please use 'Alt + Mouse Selection' or 'Alt + Shift + Arrow Keys'\n for editing in column mode.");
        }

        private void summaryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var nCol = ActiveEditor.codebox.Selection.Start.iChar + 1;
            MessageBox.Show("Words : " + WordCounting.CountWords1(ActiveEditor.codebox.Text) + "\nLetters :" + ActiveEditor.codebox.Text.Length.ToString() + "\nLines: " + ActiveEditor.codebox.Lines.Count.ToString() + "\nSel: " + ActiveEditor.codebox.Selection.ToString() + "\nCurrent Column: " + nCol.ToString(), "Summary");
        }

        private void openContainingFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string dir = Path.GetDirectoryName(ActiveEditor.Name);
                System.Diagnostics.Process.Start(dir);
            }
            catch (System.Exception ex) { MessageBox.Show("Error! File is not saved " + ex.Message); }
        }

        private void navigateForwardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.NavigateForward(); }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void navigateBackwardsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.NavigateBackward(); }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void wordWrapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try { ActiveEditor.codebox.WordWrap = wordWrapToolStripMenuItem.Checked; }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stats.Text = "Loading Preferences Dialog.....";
            Preferences p = new Preferences(this);
            p.Show();
            p.Shown += new EventHandler((obj, args) =>
            {
                Stats.Text = "Ready";
            });
        }
        private void textToSpeechToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stats.Text = "Loading Text To Speech Dialog....";
            Text_To_Speech tts = new Text_To_Speech();
            tts.SpeakingText = ActiveEditor.codebox.Text;
            tts.Show();
            Stats.Text = "Ready";
        }

        private void autoCompleteMenuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.AutoCompleteMenu.Show(true);
        }

        private void saveCurrentRecordedMacroToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog s = new SaveFileDialog();
            s.Title = "Save Macro";
            s.Filter = "Ynote Macro Files (*.ymc)|*.ymc";
            s.ShowDialog();
            File.WriteAllText(s.FileName, ActiveEditor.codebox.MacrosManager.Macros);
        }

        private void importMacroFromFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog o = new OpenFileDialog();
            o.Filter = "Ynote Macro Files (*.ymc)|*.ymc";
            o.ShowDialog();
            if (!(string.IsNullOrEmpty(o.FileName)))
                ActiveEditor.codebox.MacrosManager.Macros = File.ReadAllText(o.FileName, Encoding.Default);
        }

        private void incrementalSearcherToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IncrementalSearcher s = new IncrementalSearcher(ActiveEditor.codebox);
            s.Dock = DockStyle.Bottom;
            this.Controls.Add(s);

        }
        private void daToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime date = DateTime.Now;
                TimeSpan time = new TimeSpan(36, 0, 0, 0);
                DateTime combined = date.Add(time);
                ActiveEditor.codebox.InsertText(combined.ToString());
            }
            catch (System.Exception ex) { Console.WriteLine(ex.Message); }
        }

        private void raiseIssueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://ynoteclassic.codeplex.com/workitem/list/basic");
        }

        private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (ActiveEditor.Name != "Editor")
                {
                    FileProperties.ShowFileProperties(ActiveEditor.Name);
                }
                else
                {
                    MessageBox.Show("File is not Saved!");
                }
            }
            catch { }
        }

        private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PageSetupDialog ps = new PageSetupDialog();
            ps.Document = new System.Drawing.Printing.PrintDocument();
            ps.ShowDialog();
        }

        private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }

        private void saveAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Editor doc in dockPanel.Documents)
            {
                if (!(doc.Name == "Editor"))
                    SaveBinding(doc);
                else
                {
                    SaveFileDialog s = new SaveFileDialog();
                    s.Filter = "All Files(*.*)|*.*|Text Files(*.txt)|*.txt|XML Files (*.xml)|*.xml|XML Schema Definition File(*.xsd)|*.xsd|Log File (*.log)|*.log|HTML Document (*.html),(*.xhtml),(*.shtml)|*.html;*.xhtml;*.shtml|ASP.NET File(*.asp),(*.aspx)|*.asp;*.aspx|PHP Document (*.php)|*.php|Cascading Style Sheet (*.css)|*.css|Javascript File (*.js)|*.js|QBasic File(*.bas)|*.bas|Visual Basic File (*.vb)|*.vb|Python File (*.py)|*.py|Ruby File(*.ruby)|*.ruby|Lua File(*.lua)|Flash Actionscript file(*.as)|*.as|C# Source File(*.cs)|*.cs|C Source File(*.c)|C++ Source File (*.cpp)|*.cpp|C++ Header File(*.h)|*.h";
                    s.Title = "Save As " + doc.Text;
                    s.ShowDialog();
                    if (!string.IsNullOrEmpty(s.FileName))
                        SaveBinding(doc, "Save : " + doc.Text);
                }
            }
        }

        private void alwaysOnTopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.TopMost = true;
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            saveToolStripMenuItem_Click(sender, e);
        }

        private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    Stats.Text = "Downloading Data...";
                    Uri URL = new Uri(this.toolStripTextBox1.Text);
                    System.Net.WebClient Manager = new System.Net.WebClient();
                    Manager.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(Manager_DownloadStringCompleted);
                    Manager.DownloadStringAsync(URL);
                }
                catch (System.Exception ex) { MessageBox.Show(ex.Message); }
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            OnLoad();
            LoadPlugins();
            LoadUserSyntax(Application.StartupPath + "\\Highlighters");
           // MessageBox.Show("No. Of Encodings Supported : " + miencodingsave.DropDownItems.Count + "\r\n" + "No. of Languages : " + misyntaxhighlight.DropDownItems.Count);
        }
        private void Manager_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
        {
            Editor edit = new Editor(this);
            edit.codebox.Text = e.Result;
            edit.Text = this.toolStripTextBox1.Text;
            edit.Name = "Editor";
            edit.Show(dockPanel, DockState.Document);
            Stats.Text = "Ready";
        }

        private void fromDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog f = new FolderBrowserDialog();
            f.Description = "Import Files From Directory";
            f.ShowDialog();
            if (f.SelectedPath != null)
            {
                string[] files = Directory.GetFiles(f.SelectedPath);
                foreach (string file in files)
                {
                    OpenFile(file, 0);
                }
            }
        }

        private void searchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://support.sscorps.tk");
        }

        private void pluginCentralToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://ynoteplugins.codeplex.com");
        }

        private void removeCurrentLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ActiveEditor.codebox.Selection.Start.iLine >= 0 && ActiveEditor.codebox.Selection.Start.iLine < ActiveEditor.codebox.LinesCount)
            {
                int iLine = ActiveEditor.codebox.Selection.Start.iLine;
                int LinesCount = ActiveEditor.codebox.Lines.Count;
                ActiveEditor.codebox.RemoveLines(new List<int> { iLine });
                ActiveEditor.codebox.Selection.Start = new Place(0, Math.Max(0, Math.Min(iLine, LinesCount - 1)));
            }
        }

        private void editingModeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Press the 'Ins' Key to Switch Between Insert and Overwrite Editing Mode");
        }

        private void removeWordLeftToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Press Ctrl + Backspace Anytime to remove left word");
        }

        private void removeWordRightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Press Ctrl + Delete Anytime to remove right word");

        }

        private void goWordLeftToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Press Ctrl + Left To Go Word Left");
        }

        private void goWordRightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Press Ctrl + Right To Go Word Left");
        }

        private void goWordLeftWithSelectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Press Shift+Ctrl+Left To Go Word Left With Selection");
        }

        private void goWordRightWithSelectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Press Shift+Ctrl+Right To Go Word Right With Selection");

        }

        private void batchToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Text = "@echo off";
        }

        private void xMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Text = "<?xml version='1.0'?>";
        }

        private void hTMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Text = "<html>\n\r<head>\n\r<title></title>\n\r</head>\n\r<body>\n\r\n\r</body>\n\r</html>";
        }

        private void xHTMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Text = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>\n\r<html xmlns='http://www.w3.org/1999/xhtml'>\n\r<head>\n\r<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />\n\r<title></title>\n\r</head>\n\r</head>\n\r<body>\n\r\n\r</body>\n\r</html>";
        }

        private void cSSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Text = "/* CSS Document */ \n\r\n\rbody\n\r{\n\r}";

        }

        private void pHPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Text = "<?php ?>";
        }

        private void javascriptToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Text = "function()\n\r{\n\r\n\r}";
        }

        private void actionscriptToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Text = "package\n\r{\n\rpublic class Class1 \n\r{\n\r}\n\r}";
        }

        private void findInFilesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Components.FindInFiles f = new Components.FindInFiles();

            f.Show(dockPanel, DockState.DockBottom);
        }

        private void cloneDocumentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Editor edit = new Editor(this);
            edit.codebox.SourceTextBox = ActiveEditor.codebox;
            edit.Name = ActiveEditor.Name;
            edit.Text = ActiveEditor.Text + "[Split View]";
            edit.DockAreas = ((WeifenLuo.WinFormsUI.Docking.DockAreas)((WeifenLuo.WinFormsUI.Docking.DockAreas.Float | WeifenLuo.WinFormsUI.Docking.DockAreas.Document)));
            edit.Show(dockPanel, DockState.Float);
            edit.DockHandler.FloatPane.DockTo(dockPanel.DockWindows[DockState.Document]);
        }

        private void menuBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (menuBarToolStripMenuItem.Checked)
                Menustrip.Visible = true;
            else
                Menustrip.Visible = false;
        }

        private void systemToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            try
            {
                 Render(UITheme.System);
                 Settings.Default.UITheme = UITheme.System;
            }
            catch(Exception){
                MessageBox.Show("Tab Changes will affect in next start", "Ynote Classic");
            }
        }

        private void lightToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            try
            {
                 Render(UITheme.Light);
                 Settings.Default.UITheme = UITheme.Light;
            }
            catch(Exception){
                MessageBox.Show("Tab Changes will affect in next start", "Ynote Classic");
            }
        }
        private void darkToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            try
            {
                 Render(UITheme.Dark);
                 Settings.Default.UITheme = UITheme.Dark;
            }
            catch(Exception){
                MessageBox.Show("Tab Changes will affect in next start", "Ynote Classic");
            }
        }
        private void goNextBracketToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.GoLeftBracket('(', ')');
        }

        private void goRightBracketToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.GoRightBracket('(', ')');
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.GoLeftBracket('{', '}');
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.GoRightBracket('{', '}');
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.GoLeftBracket('[', ']');
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.GoRightBracket('[', ']');
        }
        private void revertToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ActiveEditor.Name != "Editor")
            {
                ActiveEditor.codebox.IsChanged = false;
                string ed = ActiveEditor.Name;
                ActiveEditor.Close();
                OpenFile(ed, 0);
            }
            else
                MessageBox.Show("File Is Not Saved!", "Ynote Classic", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void encryptFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ActiveEditor.Name != "Editor")
                new SS.Ynote.Classic.Utils.EncryptionDialog(ActiveEditor.Name, this, Utils.ProtectionType.Encrypt).Show();
            else
                MessageBox.Show("File Is Not Saved", "Ynote Classic");
        }

        private void decryptFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new SS.Ynote.Classic.Utils.EncryptionDialog(ActiveEditor.Name, this, Utils.ProtectionType.Decrypt).Show();
        }

        private void fileExplorerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new SS.Ynote.Classic.UI.Forms.Explorer(this).Show(dockPanel, DockState.DockLeft);
        }
        private void cToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Text = "using System;\r\n\nnamespace Default\r\n{ \r\n public class Class1\r\n     {\r\n   \r\n    }   \r\n}";
        }

        private void cCToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Text = "#include <iostream.h>\n\nusing namespace std;\n\nint Main()\n{\n\n\n\n}";
        }

        private void javaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Text = "public class Class1 {\n\n}";
        }

        private void qBasicToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.Text = "Imports System\n\nNamespace Default\n  Public Class Class1\n\n  End Class\n\nEnd Namespace";
        }

        private void navigateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Press Ctrl + Shift + N To Navigate between bookmarks\n or simply use the toolbar");
        }
            
        private void fromRTFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog o = new OpenFileDialog();
            o.Filter = "RTF Files (*.rtf)|*.rtf";
            o.ShowDialog(); if (o.FileName != "")
            {
                Editor edit = new Editor(this);
                edit.codebox.Text = ConvertToText(File.ReadAllText(o.FileName));
                edit.Name = o.FileName;
                edit.Text = Path.GetFileName(o.FileName);
                edit.Show(dockPanel, DockState.Document);
            }
        }
        static public string ConvertToText(string rtf)
        {
            using (RichTextBox rtb = new RichTextBox())
            {
                rtb.Rtf = rtf;
                return rtb.Text;
            }
        }

        private void emptyLinesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Utils.InsertLine Is = new SS.Ynote.Classic.Utils.InsertLine(ActiveEditor.codebox, SS.Ynote.Classic.Utils.InsertType.Line);
            Is.ShowDialog();
        }

        private void emptyColumnsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Utils.InsertLine Is = new SS.Ynote.Classic.Utils.InsertLine(ActiveEditor.codebox, SS.Ynote.Classic.Utils.InsertType.Column);
            Is.ShowDialog();
        }
        private void runToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (ActiveEditor.Name != "Editor")
            {
                if (ActiveEditor.codebox.IsChanged == true)
                    Save();
                try
                {
                    System.Diagnostics.Process.Start(ActiveEditor.Name);
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
            }
            else
                MessageBox.Show("File Is Not Saved!", "Runner");

        }

        private void openWithoutHighlightingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog o = new OpenFileDialog();
            o.Filter = filefilter;
            o.ShowDialog();
            if (!string.IsNullOrEmpty(o.FileName))
                OpenBinding(o.FileName);
        }

        private void zToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string HTML = ActiveEditor.codebox.Html;
            SaveFileDialog s = new SaveFileDialog();
            s.Title = "Export as HTML Document";
            s.Filter = "HTML Documents (*.html)|*.html";
            s.ShowDialog();
            if (s.FileName != null)
                File.WriteAllText(s.FileName, HTML);
        }
        private void RemoveLineEndings_Click(object sender, EventArgs e)
        {
            string text = ActiveEditor.codebox.Text;
            ActiveEditor.codebox.Text = text.Replace(System.Environment.NewLine, "");
        }
        private void Trasparent_Click(object sender, EventArgs e)
        {
            if (Trasparent.Checked == true)
                this.Opacity = 0.6;
            else if (Trasparent.Checked == false)
                this.Opacity = 1.0;
        }
        private void defaultToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            foreach (ToolStripMenuItem item1 in mienvironmenttheme.DropDownItems)
                item1.Checked = false;
            item.Checked = true;
            YnoteSettingsBase.CurrentTheme = new SS.Ynote.Classic.Themes.IDefaultTheme();
            Settings.Default.Theme = item.Name;
        }
        private void pNGToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var bmp = new Bitmap(ActiveEditor.codebox.Width, ActiveEditor.codebox.Height);
            ActiveEditor.codebox.DrawToBitmap(bmp, new Rectangle(0, 0, ActiveEditor.codebox.Width, ActiveEditor.codebox.Height));
            using (SaveFileDialog o = new SaveFileDialog())
            {
                o.Filter = "Portable Network Graphics (*.png)|*.png|JPEG (*.jpg)|*.jpg";
                o.ShowDialog();
                if (!string.IsNullOrEmpty(o.FileName))
                    bmp.Save(o.FileName);
            }
        }

        private void fileToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog o = new OpenFileDialog())
            {
                o.Filter = "All Files(*.*)|*.*";
                o.Title = "Insert File As Text";
                o.ShowDialog();
                if (!string.IsNullOrEmpty(o.FileName))
                    ActiveEditor.codebox.InsertText(File.ReadAllText(o.FileName));
            }
        }
        private void pluginManagerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new SS.Ynote.Classic.UI.Forms.PluginManager().Show();
        }

        private void macroToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int macronum = 1;
            CreateTab("Macro" + macronum, "<macros>\r\n     <item key='' />\r\n</macros>"); macronum = macronum + 1;
        }

        private void pluginToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            int pn = 1;
            CreateTab("PluginDefinition" + pn, "using System;\r\nusing System.ComponentModel.Composition;\r\nusing System.Collections.Generic;\r\nusing System.Windows.Forms;\r\n\r\nnamespace YnotePlugin\r\n{\r\n    [InheritedExport(typeof(IGeneralPlugin))]\r\n    public class Plugin" + pn + " : IGeneralPlugin\r\n    {\r\n        /// <summary>\r\n        /// Partial Host. Will Be Set By Ynote AutoMatically\r\n        /// </summary>\r\n        private IWindowHost partialhost;\r\n        /// <summary>\r\n        /// Interface Host\r\n        /// </summary>\r\n        public IWindowHost Host { get { return partialhost; } set { partialhost = value; } }\r\n        /// <summary>\r\n        /// Where to Add\r\n        /// </summary>\r\n        public AddTo AddTo { get; }\r\n        /// <summary>\r\n        /// The Menu Items To Be Added To The YnoteSettingsBase Menu.\r\n        /// </summary>\r\n        public List<ToolStripMenuItem> MenuItems { get { return items;} }\r\n        /// <summary>\r\n        /// Name of the Plugin\r\n        /// </summary>\r\n        public string Name { get { return \"Test Plugin\"; } }\r\n        /// <summary>\r\n        /// Version of the Plugin\r\n        /// </summary>\r\n        public string Version { get { return \"1.0\"; } }\r\n        /// <summary>\r\n        /// Author of the Plugin\r\n        /// </summary>\r\n        public string Author { get { return \"Ynote Classic\"; } }\r\n        /// <summary>\r\n        /// Description of the Plugin\r\n        /// </summary>\r\n        public string Description { get { return \"Sample Plugin.\"; } }\r\n        List<ToolStripMenuItem> items;\r\n        ///<summary>\r\n        /// Constructor\r\n        ///</summary>\r\n        public void Dispose()\r\n        {\r\n        }\r\n        \r\n    }\r\n}\r\n"); pn = pn + 1;
            Highlighter.SetSyntax(Language.CSharp, ActiveEditor.codebox.Range);
        }

        private void languageDefinitionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int ld = 1;
            CreateTab("LanguageDefinition" + ld, "<doc>\r\n     <style/>\r\n     <rule/>\r\n</doc>"); ld = ld + 1;
            Highlighter.SetSyntax(Language.Xml, ActiveEditor.codebox.Range);
        }

        private void pluginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pluginManagerToolStripMenuItem_Click(sender, e);
        }

        private void uIThemeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog o = new OpenFileDialog()) {
                o.Title = "Import Theme";
                o.Filter = "Ynote Environment Themes(*.dll)|*.dll";
                o.ShowDialog();
                if (!string.IsNullOrEmpty(o.FileName))
                    ImportTheme(o.FileName);
            }
        }
        /// <summary>
        /// Import Style Theme
        /// </summary>
        /// <param name="theme"></param>
        void ImportTheme(string theme) 
        {
            File.Copy(theme, Application.StartupPath + @"\Themes");
            this.mienvironmenttheme.Dispose();
            LoadThemes();
        }
        private void fileNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.InsertText(ActiveEditor.Text);
        }
        private void fullFileNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ActiveEditor.Name != "Editor")
                ActiveEditor.codebox.InsertText(ActiveEditor.Name);
            else
                ActiveEditor.codebox.InsertText(@"%ynote%\" + ActiveEditor.Text);
        }
        private void professionalSystemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                 Render(UITheme.ProfessionalSystem);
                 Settings.Default.UITheme = UITheme.ProfessionalSystem;
            }
            catch(Exception){
                MessageBox.Show("Tab Changes will affect in next start", "Ynote Classic");
            }
        }
        private void insertToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.InsertLinePrefix(ActiveEditor.codebox.CommentPrefix);
        }

        private void removeToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ActiveEditor.codebox.RemoveLinePrefix(ActiveEditor.codebox.CommentPrefix);
        }
        #endregion

    }
}